/**
 * 行政老师删除岗位
 * 
 * 报名该岗位的学生的状态都改为未报名
 * 
 * /xingzheng/company/del
 */

 
const Companies = require('../../../models/companys')
const InternshipStudent = require('../../../models/internshipStudents')
const MySQLManager = require('../../../utils/MySQLManager');
const sequelize = MySQLManager
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')

const del = async function (req, res) {
    try {

        if (!req.body.company_id) {
            $.sendMessage(res, 1015, 'company_id 不能缺少')
            return
        }

        // 删除事务
        return sequelize.transaction(function (t) {
            
            return Companies.destroy(
            {
                where: {
                    id: req.body.company_id
                }
            }
            , 
            {transaction: t}).then(function (user) {
            return InternshipStudent.update(
                {
                    company_name: '',
                    company_id: '',
                    teacher_user_name: '',
                    teacher_user_fullname: '',
                    student_status: '0'
                },
                {
                    where: {
                        company_id: req.body.company_id
                    }
                }
                , {transaction: t});
              })
            }).then(function (result) {
                res.json({
                    code: 200,
                    message: '删除成功',
                    result: result
                })
                utils.create_a_log(req, '岗位管理', '删除')
                return
            }).catch(error => {
                // 异常
                res.json({
                    code: 0000,
                    message: '代码错误，请联系管理员' + error
                })
                       
                utils.create_a_log(req, '岗位管理', '删除', '失败')
                return
            })    
    }
    catch(error) {
        res.json({
            message: "抓到异常" + error,
        })
        utils.create_a_log(req, '岗位管理', '删除', '失败')
        return
    } 
}
    
module.exports = del